package cn.haoxsf.app.common.api;

import cn.haoxsf.app.common.api.enums.IServiceEnum;
import cn.haoxsf.app.common.api.enums.SystemExceptionEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @program: app
 * @description: 前端返回基类
 * @author: Ele qvbanfm@qq.com
 * @create: 2020-01-06 09:52
 **/
@Data
public class CommonResult<T> {


    @ApiModelProperty(value = "状态码",example = "200")
    private Integer code;
    @ApiModelProperty(value = "提示信息",example = "请求成功")
    private String msg;
    private T result;

    public CommonResult(Integer code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public CommonResult(IServiceEnum serviceEnum, T result) {
        this.code = serviceEnum.getCode();
        this.msg = serviceEnum.getMsg();
        this.result = result;
    }

    public static <T> CommonResult<T> success(T result) {
        return new CommonResult<T>(SystemExceptionEnum.SUCCESS, result);
    }

    public static CommonResult success() {
        return new CommonResult(SystemExceptionEnum.SUCCESS, true);
    }

    public static CommonResult error(IServiceEnum serviceEnum) {
        return new CommonResult(serviceEnum.getCode(), serviceEnum.getMsg(), false);
    }

    public static CommonResult error(Integer code, String msg) {
        return new CommonResult(code, msg, false);
    }
}
